package cc.rengu.igas.channel.nucc.core.service.trans;

import cc.rengu.igas.channel.nucc.common.constant.NuccParamConstant;
import cc.rengu.igas.channel.nucc.common.constant.NuccTreeNodeConstant;
import cc.rengu.igas.channel.nucc.common.enums.RespCodeEnum;
import cc.rengu.igas.channel.nucc.core.realize.NuccService;
import cc.rengu.igas.channel.nucc.core.realize.impl.NuccServiceImpl;
import cc.rengu.igas.channel.nucc.facade.request.TransStateQueryAcqRequest;
import cc.rengu.igas.channel.nucc.facade.request.TransStateQueryRequest;
import cc.rengu.igas.channel.nucc.facade.response.TransStateQueryAcqResponse;
import cc.rengu.igas.channel.nucc.facade.response.TransStateQueryResponse;
import cc.rengu.oltp.service.base.BizBaseService;
import cc.rengu.oltp.service.model.RiskMonitorInfo;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.dao.FinanceTxnMapper;
import cc.rengu.oltp.service.common.dao.impl.FinanceTxnMapperImpl;
import cc.rengu.oltp.service.common.entity.FinanceTxn;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.annotation.BIZ_PROCESS;
import cc.rengu.oltp.utility.util.*;

/**
 * 交易状态查询
 */
public class EpccTransStateQueryService extends BizBaseService {
    @Override
    protected Object beforeBizProcess() throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        TransStateQueryRequest request = new TransStateQueryRequest();
        ConvertUtil.convertOutput(request);

        xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.FRONT_SEQ_NUM, request.getHeader().getTraceNo());
        return request;
    }

    @Override
    protected BizResponse callBizService(Object request) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        TransStateQueryRequest innerRequest = (TransStateQueryRequest) request;
        xmlTreeUtil.setXmlTreeStringValue(NuccTreeNodeConstant.EPCC_ISSR_ID, innerRequest.getInstgId());

        TransStateQueryAcqRequest acqRequest = initTransStateQueryAcqRequest(xmlTreeUtil, innerRequest);
        NuccService nuccService = new NuccServiceImpl();
        TransStateQueryAcqResponse acqResponse = nuccService.transStateQuery(acqRequest);
        BizResponse bizResponse = new BizResponse();
        bizResponse.setRspSysId(NuccParamConstant.IGAS_SYS_ID);
        if (!NuccParamConstant.EPCC_SUCCESS_CODE.equals(acqResponse.getSysRtnCd())) {
            rglog.error("调用网联接口失败，sysRtnCd=[{}],sysRtnDesc=[{}]", acqResponse.getSysRtnCd(), acqResponse.getSysRtnDesc());
            bizResponse.setRespCode(acqResponse.getSysRtnCd());
            bizResponse.setRespDesc(acqResponse.getSysRtnDesc());
        } else {
            TransStateQueryResponse transStateQueryResponse = new TransStateQueryResponse();
            transStateQueryResponse.setOriTrxStatus(acqResponse.getOriTrxStatus());
            transStateQueryResponse.setOriTrxAmt(acqResponse.getOriTrxAmt());
            transStateQueryResponse.setOriBatchId(acqResponse.getOriBatchId());
            transStateQueryResponse.setOriSignNo(acqResponse.getOriSgnNo());
            transStateQueryResponse.setPyerAcctTp(acqResponse.getPyerAcctTp());
            transStateQueryResponse.setPyerAcctId(acqResponse.getPyerAcctId());
            transStateQueryResponse.setPyerAcctNm(acqResponse.getPyerAcctNm());

            bizResponse.setRespCode(acqResponse.getBizStsCd());
            bizResponse.setRespDesc(acqResponse.getBizStsDesc());
            bizResponse.setResult(transStateQueryResponse);
        }

        return bizResponse;
    }

    @Override
    protected void afterBizProcess(BizResponse response) throws Exception {
        TransStateQueryResponse transStateQueryResponse = (TransStateQueryResponse) response.getResult();
        ConvertUtil.convertInput(transStateQueryResponse);
    }

    @Override
    protected RiskMonitorInfo getRiskMonitorInfo() {
        return null;
    }

    private TransStateQueryAcqRequest initTransStateQueryAcqRequest(XmlTreeUtil xmlTreeUtil, TransStateQueryRequest innerRequest) throws Exception {
        TransStateQueryAcqRequest request = new TransStateQueryAcqRequest();
        request.setInstgId(innerRequest.getInstgId());
        request.setOriTrxCtgy(innerRequest.getOriTrxCtgy());

        /*将原交易渠道流水转换为原网联通道流水*/
        String instId = innerRequest.getHeader().getInstId();
        String oriTrxId = innerRequest.getOriTrxId();
        String oriTrxDtTm = innerRequest.getOriTrxDtTm();
        FinanceTxnMapper txnMapper = new FinanceTxnMapperImpl();
        FinanceTxn orgFinanceTxn = txnMapper.selectFinanceTxnInfoByFrontSeqNum(instId, oriTrxDtTm, oriTrxId, NuccParamConstant.TUNL_ID_NUCC, innerRequest.getHeader().getChanlId());
        if (null == orgFinanceTxn) {
            rglog.error("根据前端流水号[{}]查询原交易失败!", oriTrxId);
            throw new BizException(RespCodeEnum.ORIG_TXN_NOT_FOUND.getRespCode(), RespCodeEnum.ORIG_TXN_NOT_FOUND.getRespDesc());
        }
        request.setOriTrxId(orgFinanceTxn.getRemark1());

        /*原交易日期时间从原交易系统流水号获取*/
        String srcDateTime = request.getOriTrxId().substring(0, 8) + request.getOriTrxId().substring(12, 18);
        String trxDateTime = DateUtil.dateStringFormatConvert(srcDateTime, "yyyyMMddHHmmss", "yyyy-MM-dd'T'HH:mm:ss");
        request.setOriTrxDtTm(trxDateTime);

        return request;
    }
}
